(function() {
	Wallet.widget.JSONDataTable = function(el, config) {
		var columnDefs = this.columnDefs;
		
		this.dataUri = config.dataUri;
		var dataSource = this.dataSource = this._getDataSource();
		
		Wallet.widget.JSONDataTable.superclass.constructor.call(this, el, columnDefs, dataSource, config);	
		
		this.initDomElements();
		
		this.initListeners();	
	};
	
	Wallet.widget.AccountRecordDataTable = function(el, config) {
		Wallet.widget.AccountRecordDataTable.superclass.constructor.call(this, el, config);	
	};
	
	var Lang = YAHOO.lang,
		DataTable = YAHOO.widget.DataTable,
		DataSource = YAHOO.util.DataSource,
		Dom = YAHOO.util.Dom,
		JSONDataTable = Wallet.widget.JSONDataTable,
		AccountRecordDataTable = Wallet.widget.AccountRecordDataTable;
		
	Lang.extend(JSONDataTable, DataTable, {
		initAttributes: function(attr) {
			JSONDataTable.superclass.initAttributes.call(this, attr);
			
			this.setAttributeConfig("dataUri", {
	            value: attr.dataUri,
	            method: function(value) {
	            	this.dataUri = value;
	            },
	            validator: function(value) {
	            	return Lang.isString(value);
	            }
        	});
		},
		initDomElements: function() {			
			YAHOO.util.Dom.addClass(this._elTable, "full-width");
		},
		initListeners: function() {
			this.addListener("rowMouseoverEvent", this.onEventHighlightRow);   
			this.addListener("rowMouseoutEvent", this.onEventUnhighlightRow);
			
//			this.addListener("rowClickEvent", this.onEventSelectRow);
		},
		onEventSelectRow: function(args) {		
			JSONDataTable.superclass.onEventSelectRow.call(this, args);
				
			var row = args.target;
			var record = this.getRecord(row);
			var date = record.getData("date");			
		},
		load: function(date) {
			this.dataSource.sendRequest(date, this.onDataReturnInitializeTable, this);
		},
		_getDataSource: function() {	
			if (!this.dataSource) {
//				Dom.setStyle(dataSourceEl, "display", "none");
				var dataSource = new DataSource(this.dataUri);		
				dataSource.responseType = this.responseType;
				dataSource.responseSchema = this.responseSchema;
				dataSource.connXhrMode = this.connXhrMode;
				
				this.dataSource = dataSource;
			}	
			
			return this.dataSource;
		}
	});
	
	Lang.extend(AccountRecordDataTable, JSONDataTable, {
		columnDefs: [{
				key: "checked",
				label: "",
				formatter: DataTable.formatCheckbox,
				width: "24px"
			}, {
				key: "date", 
				label: "Date", 
				formatter: DataTable.formatDate,
				sortable: true
			}, {
				key: "category", 
				label: "Category", 
				sortable: true
			}, {
				key: "amount", 
				label: "Amount", 
//				formatter: Money.formatAccountRecordAmount, 
				sortable: true
			}, {
				key: "description", 
				label: "Description", 
				sortable: true
			}
	    ],
	    responseType: DataSource.TYPE_JSON,
	    responseSchema: {
	    	resultsList: "result",
	        fields: [{
	        	key: "id"
	        }, {
	        	key: "date"/*,
	        	parser: DataSource.parseDate*/
	        }, {
	        	key: "category"
	        }, {
	        	key: "amount",
	        	parser: DataSource.parseNumber
	        }, {
	        	key: "description"
	        }, {
	        	key: "type"
	        }/*, {
	        	key: "checked"
	        }*/]
	    },
	    connXhrMode: "queueRequests"
	});
}());